package com.nantian.logic.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 一些突然想到的Map的操作
 * Created by AileYoung on 2018/2/28.
 */
public class MapUtils {
    /**
     * key值判空,返回true则为空
     */
    public static boolean checkKeyIsNull(Map<String, Object> map, String... keys) {
        for (String key : keys
                ) {
            if (!map.containsKey(key) || StringUtils.isBlank(map.get(key).toString()))
                return true;
        }
        return false;
    }

    /**
     * 判断map是否存在空值
     */
    public static boolean checkExistNullValue(Map map) {
        for (Object value : map.values()) {
            if (value == "" || value == null)
                return true;
        }
        return false;
    }

    /**
     * 页码判空,返回true则为空
     */
    public static boolean checkPageIsNull(Map<String, Integer> map) {
        return !map.containsKey("pageNo") ||
                !map.containsKey("pageSize") ||
                StringUtils.isBlank(String.valueOf(map.get("pageNo"))) ||
                StringUtils.isBlank(String.valueOf(map.get("pageSize")));
    }

    /**
     * 自定义select中的map拼装
     */
    public static Map<String, String> setSelectStatement(String select, String where) {
        Map<String, String> map = new HashMap<>();
        map.put("select", select);
        map.put("where", where);
        return map;
    }

    /**
     * 把mapA的key的值转移到mapB中，并移除mapA的key值
     */
    public static void moveMapValue(Map<String, Object> mapA, Map<String, Object> mapB, String key) {
        if (mapA.containsKey(key)) {
            mapB.put(key, mapA.get(key));
            mapA.remove(key);
        } else {
            //不存在key值时置为空
            mapB.put(key, "");
        }
    }

    public static void main(String[] args) {
        Map<String, Object> A = new HashMap<>();
        Map<String, Object> B = new HashMap<>();

        A.put("C", "A");
        B.put("B", "B");
        System.out.println("A:" + A.toString() + "|||" + "B:" + B.toString());

        MapUtils.moveMapValue(A, B, "A");
        System.out.println("A:" + A.toString() + "|||" + "B:" + B.toString());

    }

}
